import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Story } from './stories/entities/story';
import { StoryModule } from './stories/story.module';
import { Image } from './stories/entities/image';
import { UserStore } from './users/entities/userSotre';
import { StoryLike } from './stories/entities/storyLike';
import { User } from './users/entities/user';
import { UserModule } from './users/user.module';
import { CacheModule } from '@nestjs/cache-manager';
import { AuthModule } from './auth/auth.module';
import { AuthGuard } from './auth/auth.guard';
import { APP_GUARD } from '@nestjs/core';
import { MulterModule } from '@nestjs/platform-express';
import { diskStorage } from 'multer';

@Module({
  imports: [
    MulterModule.register({
      dest: './uploads',
      storage: diskStorage({
        destination: './uploads',
        filename: (req, file, cb) => {
          // Generate a unique filename
          const filename = `${new Date().getTime()}-${file.originalname}`;
          cb(null, filename);
        },
      }),
      limits: {
        fileSize: 1024 * 1024 * 5, // 5MB
      },
    }),
    CacheModule.register({ isGlobal: true }),
    TypeOrmModule.forRoot({
      type: 'mysql',
      host: 'localhost',
      port: 3306,
      username: 'root',
      password: 'root',
      database: 'news_day',
      logging: true,
      cache: false,
      entities: [Story, Image, User, UserStore, StoryLike],
      synchronize: false,
      autoLoadEntities: true,
    }),
    StoryModule,
    UserModule,
    AuthModule,
  ],
  controllers: [AppController],
  providers: [
    AppService,
    {
      provide: APP_GUARD,
      useClass: AuthGuard,
    },
  ],
})
export class AppModule {}
